package edu.stanford.bmir.protege.web.client.rpc.stkosutil.common;

import java.io.Serializable;
import java.util.HashMap;

import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilConstants;

public class MetaItemList implements Serializable{

	private HashMap<String, String> hMetaItems = null;
	private String leadingTag = "";
	public void parse(String text){
		hMetaItems = new HashMap<String,String>();
		String[] items =  text.split(";");
		for (String item: items){
			int pos = item.indexOf(":");
			if (pos!=-1){
				String tag = item.substring(0, pos);
				tag = tag.trim();
				String value = "";
				if (pos<item.length()-1){
					value = item.substring(pos+1);
					value = value.trim();
				}
				if (tag.length()>0 && leadingTag.length()==0){
					leadingTag = tag;
				}
				if (value.length()>0){
					hMetaItems.put(tag, value);
				}
			}
		}
	}	
	
	public String getValue(String tag){
		return hMetaItems.get(tag);
	}
	public String getLeadingTag(){
		return this.leadingTag;
	}
	public boolean tagExists(String tag){
		return hMetaItems.get(tag)!=null;
	}
		
	public static boolean isPreProjectMappingFlag(String annotationValue, String preProjectName){
 		MetaItemList metaItemList = new MetaItemList();
 		metaItemList.parse(annotationValue);
 		if (metaItemList.tagExists(UtilConstants.FLAG_MAPPING_NO)){
 			String tempRefProject = metaItemList.getValue(UtilConstants.FLAG_REF_PROJECT_ID);
 			if (tempRefProject!=null && tempRefProject.compareTo(preProjectName)==0){
 				return true;
 			}
 		}
 		return false;
 		
 	}
	
	public static boolean isCutFlag(String annotationValue){
 		MetaItemList metaItemList = new MetaItemList();
 		metaItemList.parse(annotationValue);
 		if (metaItemList.tagExists(UtilConstants.FLAG_CUTTING)){
 			String value = metaItemList.getValue(UtilConstants.FLAG_CUTTING);
 			if (value!=null && value.equalsIgnoreCase("Y")){
 				return true;
 			}
 		}
 		return false;
 	}
	
	public static boolean isMergedFlag(String annotationValue){
 		MetaItemList metaItemList = new MetaItemList();
 		metaItemList.parse(annotationValue);
 		if (metaItemList.tagExists(UtilConstants.FLAG_MERGED)){
 			String value = metaItemList.getValue(UtilConstants.FLAG_MERGED);
 			if (value!=null && value.equalsIgnoreCase("Y")){
 				return true;
 			}
 		}
 		return false;
 	}
	
	public static boolean isEnrichedFlag(String annotationValue){
 		MetaItemList metaItemList = new MetaItemList();
 		metaItemList.parse(annotationValue);
 		if (metaItemList.tagExists(UtilConstants.FLAG_ENRICHED)){
 			String value = metaItemList.getValue(UtilConstants.FLAG_ENRICHED);
 			if (value!=null && value.equalsIgnoreCase("Y")){
 				return true;
 			}
 		}
 		return false;
 	}
	
	public static int extractPreProjectMappingNumber(String annotationValue, String preProjectName){
 		MetaItemList metaItemList = new MetaItemList();
 		metaItemList.parse(annotationValue);
 		if (metaItemList.tagExists(UtilConstants.FLAG_MAPPING_NO)){
 			String tempRefProject = metaItemList.getValue(UtilConstants.FLAG_REF_PROJECT_ID);
 			if (tempRefProject!=null && tempRefProject.compareTo(preProjectName)==0){
 				String sNumber= metaItemList.getValue(UtilConstants.FLAG_MAPPING_NO);
 				if (sNumber!=null){
 					return Integer.parseInt(sNumber);
 				}
 			}
 		}
 		return UtilConstants.UNKNOWN_INT_VALUE;
 	}
	public static boolean annotationMatch(String annotation1, String annotation2){
		MetaItemList metaItemList1 = new MetaItemList();
		metaItemList1.parse(annotation1);
		MetaItemList metaItemList2 = new MetaItemList();
		metaItemList2.parse(annotation2);
		if (metaItemList1.getLeadingTag().compareTo(metaItemList2.getLeadingTag())==0){
			String preProject1 = metaItemList1.getValue(UtilConstants.FLAG_REF_PROJECT_ID);
			String preProject2 = metaItemList2.getValue(UtilConstants.FLAG_REF_PROJECT_ID);
			if (preProject1!=null || preProject2!=null){
				  if (preProject1!=null && preProject2!=null && preProject1.compareTo(preProject2)==0){
					  return true;
				  }else{
					  return false;
				  }
			}else{
				return true;
			}	
			
		}else{
			return false;
		}
	}

}
